package com.pikaqiu.warehouse.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pikaqiu.warehouse.config.MiniConstant;
import com.pikaqiu.warehouse.domain.common.ReqCommonParams;
import com.pikaqiu.warehouse.domain.dto.CompanyDto;
import com.pikaqiu.warehouse.domain.dto.CompanyIndexDto;
import com.pikaqiu.warehouse.domain.dto.HomeIndexDto;
import com.pikaqiu.warehouse.domain.entity.Company;
import com.pikaqiu.warehouse.domain.entity.Materials;
import com.pikaqiu.warehouse.domain.entity.Products;
import com.pikaqiu.warehouse.domain.entity.UserCompany;
import com.pikaqiu.warehouse.domain.enums.UserCompanyFlagEnum;
import com.pikaqiu.warehouse.domain.req.CompanyForm;
import com.pikaqiu.warehouse.exception.MiniServiceException;
import com.pikaqiu.warehouse.mapper.CompanyMapper;
import com.pikaqiu.warehouse.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Administrator
* @createDate 2024-08-14 14:44:32
*/
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company>
    implements CompanyService {

    @Resource
    private UserService userService;

    @Resource
    private ProductsService productsService;

    @Resource
    private MaterialsService materialsService;

    @Resource
    private UserCompanyService userCompanyService;

    @Override
    public List<CompanyDto> listUserCompany() {
        ReqCommonParams reqCommonParams = userService.checkUser();
        Integer companyId = reqCommonParams.getCompanyId();
        List<Integer> userCompanyIdList = reqCommonParams.getUserCompanyIdList();
        List<Company> companyList = listByIds(userCompanyIdList);
        if (CollectionUtil.isEmpty(companyList)) {
            companyList.add(getById(MiniConstant.DEFAULT_COMPANY_ID));
        }
        List<CompanyDto> companyDtoList = companyList.stream().map(CompanyDto::new).collect(Collectors.toList());
        companyDtoList.stream().filter(o -> Objects.equals(o.getId(), companyId)).forEach(o -> o.setIsCurrent(true));
        return companyDtoList;
    }

    @Override
    public CompanyIndexDto info() {
        ReqCommonParams reqCommonParams = userService.checkUser();
        Integer companyId = reqCommonParams.getCompanyId();
        String nickName = reqCommonParams.getNickName();
        Company company = getById(companyId);
        List<Products> productList = productsService.listAllProduct(companyId);
        BigDecimal productNum = productList.stream().map(Products::getStock).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<Materials> materialsList = materialsService.listAllMaterials(companyId);
        BigDecimal materialsNum = materialsList.stream().map(Materials::getStock).reduce(BigDecimal.ZERO, BigDecimal::add);
        return new CompanyIndexDto(company, nickName, productNum, materialsNum);
    }

    @Override
    @Transactional
    public void change(Integer id) {
        ReqCommonParams reqCommonParams = userService.checkUser();
        List<Integer> userCompanyIdList = reqCommonParams.getUserCompanyIdList();
        userCompanyService.update(new UpdateWrapper<UserCompany>()
            .set("current_flag", false)
            .in("company_id", userCompanyIdList));
        userCompanyService.update(new UpdateWrapper<UserCompany>()
            .set("current_flag", true)
            .eq("company_id", id)
            .eq("user_id", reqCommonParams.getUserId()));

    }

    @Override
    @Transactional
    public void saveCompany(CompanyForm companyForm) {
        ReqCommonParams reqCommonParams = userService.checkUser();
        Company company = companyForm.toCompany();
        company.setManagerId(reqCommonParams.getUserId());
        company.setCode("C" + System.currentTimeMillis() + reqCommonParams.getUserId());
        save(company);
        UserCompany userCompany = new UserCompany();
        userCompany.setCompanyId(company.getId());
        userCompany.setUserId(reqCommonParams.getUserId());
        userCompany.setCurrentFlag(true);
        userCompanyService.save(userCompany);
    }

    @Override
    public HomeIndexDto homeInfo() {
        ReqCommonParams reqCommonParams = userService.checkUser();
        Integer companyId = reqCommonParams.getCompanyId();
        Integer userId = reqCommonParams.getUserId();
        String nickName = reqCommonParams.getNickName();
        List<UserCompany> userCompanyList = userCompanyService.listByCompanyId(companyId);
        if (CollectionUtil.isEmpty(userCompanyList)) {
            Company company = getById(MiniConstant.DEFAULT_COMPANY_ID);
            return new HomeIndexDto(company, nickName, UserCompanyFlagEnum.NORMAL.getDesc(), 1);
        }
        int userNum = userCompanyList.size();
        Integer userFlag = userCompanyList.stream().filter(o -> Objects.equals(o.getUserId(), userId))
            .map(UserCompany::getUserFlag).findFirst().orElseThrow(() -> new MiniServiceException("数据异常"));
        String userRole = UserCompanyFlagEnum.getDescByCode(userFlag);
        Company company = getById(companyId);
        return new HomeIndexDto(company, nickName, userRole, userNum);
    }
}




